#include <iostream>
#include <uv.h>
#include <stdio.h>
#include <unistd.h>

uv_loop_t *loop;
uv_async_t async;

double percentage;

void print(uv_async_t *handle)
{
    printf("thread id: %ld, value is %ld\n", uv_thread_self(), (long long)handle->data);
}

void run(uv_work_t *req)
{
    auto count = (long long)req->data;
    for (int index = 0; index < count; index++)
    {
        printf("run thread id: %ld, index: %d\n", uv_thread_self(), index);
        async.data = (void *)(long long)index;
        uv_async_send(&async);
//        sleep(1);
    }
}

void after(uv_work_t *req, int status)
{
    printf("done, thread id: %ld\n", uv_thread_self());
    uv_close((uv_handle_t *)&async, nullptr);
}

int main()
{
    printf("main thread id: %ld\n", uv_thread_self());
    loop = uv_default_loop();

    uv_work_t req;
    int size = 5;
    req.data = (void *)(long long)size;

    uv_async_init(loop, &async, print);
    uv_queue_work(loop, &req, run, after);

    return uv_run(loop, UV_RUN_DEFAULT);
}
